<template>
  <div class="page-box" id="page-box">
    <div class="page-total">共{{total}}条</div>
    <div
      class="page-left"
      @click="handle(active-1)"
      :class="{cursorLX: pageList.length==1||active==1}"
    ></div>
    <!-- {{minnum}} -->
    <div
      class="page-item"
      v-if="item>=minnum&&item<=maxnum"
      @click="handle(item)"
      :class="{active:item==active}"
      v-for="item in  pageList"
    >{{item}}</div>
    <!-- {{maxnum}} -->
    <div
      class="page-right"
      @click="handle(active+1)"
      :class="{cursorRX: pageList.length==1||active == pageNum}"
    ></div>
  </div>
</template>
<script>
export default {
  name: "page",
  props: {
    current: {
      type: Number,
      default: 1
    },
    total: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      active: this.current
    };
  },
  methods: {
    handle(d) {
      console.log(d);
      if (d > this.pageNum) {
        return;
      }
      if (d < 1) {
        return;
      }
      if (d == this.active) {
        return;
      } else {
        this.active = d;
        this.$emit("on-change", d);
      }
    }
  },
  computed: {
    pageNum() {
      if (this.total < 11) {
        return 1;
      } else {
        return Math.ceil(this.total / 10);
      }
    },
    minnum() {
      if (this.pageNum == this.active && this.pageNum > 5) {
        return this.pageNum - 4;
      }
      if (this.pageNum > 5) {
        if (this.pageNum - this.active == 1) return this.active - 3;
        else if (this.active - 2 > 1) return this.active - 2;
        else return 1;
      }
      if (this.pageNum <= 5) {
        return 1;
      }
    },
    maxnum() {
      if (this.pageNum == this.active && this.pageNum > 5) {
        return this.active;
      }
      if (this.pageNum > 5) {
        if (this.active < 3) {
          return 5;
        } else if (this.active + 2 < this.pageNum) return this.active + 2;
        else return this.pageNum;
      } else {
        return this.pageNum;
      }
    },
    pageList() {
      var list = [];
      for (let i = 0; i < this.pageNum; i++) {
        list.push(i + 1);
      }
      return list;
    }
  }
};
</script>
<style scoped>
#page-box {
}
#page-box div {
  display: inline-block;
}
.page-total {
  color: #666;
}
.page-box {
  height: 60px;
  display: flex;
  align-items: center;
  text-align: right;
  justify-content: center;
  /* flex-direction: row-reverse; */
}
.page-left {
  width: 40px;
  cursor: pointer;
  height: 40px;
  background-image: url("../static/img/left.png");
  background-repeat: no-repeat;
  background-size: 20px 20px;
  background-position: center center;
}
.page-left:hover {
  background-image: url("../static/img/left1.png");
}
.page-right {
  width: 40px;
  cursor: pointer;
  height: 40px;
  background-image: url("../static/img/right1.png");
  background-repeat: no-repeat;
  background-size: 20px 20px;
  background-position: center center;
}
.page-right:hover {
  background-image: url("../static/img/right.png");
}
.page-item {
  width: 30px;
  cursor: pointer;
  height: 30px;
  text-align: center;
  line-height: 30px;
  background: #f2f2f2;
  color: #000;
  margin: 0 3px;
}
.cursorRX,
.cursorLX {
  cursor: no-drop;
}
.cursorLX:hover {
  background-image: url("../static/img/left.png");
}
.active {
  border: 1px solid #2d8cf0;
  color: #2d8cf0;
}
.cursorRX:hover {
  background-image: url("../static/img/right1.png");
}
</style>

